package week12;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import week12.config.TopicExchangeProducer;
import week12.entity.Order;

import java.util.UUID;

//
@EnableAspectJAutoProxy
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class DemoApplication implements ApplicationRunner {
    public static void main(String[] args) throws InterruptedException {
        //docker run -d --hostname my-rabbit --name rabbit -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=admin -p 15672:15672 -p 5672:5672 -p 25672:25672 -p 61613:61613 -p 1883:1883 rabbitmq:management
        ConfigurableApplicationContext ctx = new SpringApplicationBuilder(DemoApplication.class)
                .web(WebApplicationType.NONE).run(args);

    }
    @Autowired
    TopicExchangeProducer producer;
    @Override
    public void run(ApplicationArguments args) {
        //生产者
        new Thread(() -> {
            while (true) {
                int size = 3;
                for (int i = 0; i < size; i++) {
                    Order ord = new Order();
                    ord.setOrderSn(UUID.randomUUID().toString().substring(0, 16));
                    ord.setOrderStatus("0");
                    ord.setUserId(1L);
                    ord.setIsPay(1);
                    producer.send(JSON.toJSONString(ord),"test");
                }
                System.out.println("生产了" + size + "个订单");
                //orderService.saveBatch(orderList);

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }

}
